//
//  ByCellProductDetailProductionDescription.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/2.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellProductDetailProductionDescription.h"

@implementation ByCellProductDetailProductionDescription
{
    UILabel     *_currentPriceLabel;    // 当前价格
    UILabel     *_originPriceLabel;     // 原价
    UILabel     *_descLabel;            // 标题
    UIView      *_tagsView;             // 标签
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    // 现价
    _currentPriceLabel = [[UILabel alloc] init];
    _currentPriceLabel.adjustsFontSizeToFitWidth = true;
    _currentPriceLabel.font = FONT(18);
    _currentPriceLabel.textColor = BY_RED_COLOR;
    _currentPriceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_currentPriceLabel];
    
    // 原价
    _originPriceLabel = [[UILabel alloc] init];
    _originPriceLabel.adjustsFontSizeToFitWidth = true;
    _originPriceLabel.font = FONT(14);
    _originPriceLabel.textColor = [UIColor lightGrayColor];
    _originPriceLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_originPriceLabel];
    
    // 描述
    _descLabel = [[UILabel alloc] init];
    _descLabel.numberOfLines = 0;
    _descLabel.font = FONT(15);
    _descLabel.textColor = [UIColor blackColor];
    _descLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_descLabel];
    
    // 标签
    _tagsView = [UIView new];
    [self.contentView addSubview:_tagsView];
    
    // 布局
    [_currentPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@10);
    }];
    [_originPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-10);
        make.bottom.equalTo(_currentPriceLabel);
    }];
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_currentPriceLabel);
        make.right.equalTo(_originPriceLabel);
        make.top.equalTo(_currentPriceLabel.mas_bottom).offset(5);
        make.bottom.equalTo(self.contentView).offset(-10);
        
    }];
}

- (void)setModel:(ByModelProductDetail *)model {
    
    _model = model;
    
    // 现价
    _currentPriceLabel.text = [NSString stringWithFormat:@"￥%.2lf", _model.price];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_currentPriceLabel.text];
    [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:[_currentPriceLabel.text rangeOfString:@"￥"]];
    [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:NSMakeRange(_currentPriceLabel.text.length - 2, 2)];
    _currentPriceLabel.attributedText = attrStr;
    // 原价
    _originPriceLabel.text = [NSString stringWithFormat:@"￥%.2lf", _model.originPrice];
    attrStr = [[NSMutableAttributedString alloc] initWithString:_originPriceLabel.text];
    [attrStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle) range:[_originPriceLabel.text rangeOfString:_originPriceLabel.text]];
    _originPriceLabel.attributedText = attrStr;
    // 描述
    _descLabel.text = _model.desc;

    // 设置标签
    [self createTags];
}

- (void)createTags {
    
    // 动态创建标签视图
    [_tagsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _tagsView.left = _currentPriceLabel.right + 5;
    _tagsView.height = 20;
    _tagsView.y = _currentPriceLabel.y;
    CGFloat x = 0, y, w, h;
    for (int i = 0; i < _model.tags.count; i ++) {
        y = 0;
        w = [_model.tags[i] getSizeOfString:12].width + 10;
        h = _tagsView.height;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        label.text = _model.tags[i];
        label.font = FONT(12);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = BY_RED_COLOR;
        label.layer.cornerRadius = 4;
        label.layer.masksToBounds = true;
        label.layer.borderColor = BY_RED_COLOR.CGColor;
        label.layer.borderWidth = 1;
        [_tagsView addSubview:label];
        
        x += w + 5;
    }
    _tagsView.width = _tagsView.subviews.lastObject.right;
}




@end
